import axios from 'axios'
import store from '@/store/store.js'

// api的base_url
export const baseURL = `/moon`;

// 创建axios实例
const axiosInstance = axios.create({
	baseURL,
	timeout: 100000 // 请求超时时间
})

// request
axiosInstance.interceptors.request.use(
	config => {
		
		config.headers['access_token']  = store.state.token;
		config.headers['_session_user'] = store.state.sessionUser;
		config.headers['_session_org']  = store.state.sessionOrg;
		
		return config;
	},
	error => {
		return Promise.reject('error');
	}
)
// response
const STATUS = 200;
axiosInstance.interceptors.response.use(
	//走通接口的时候
	response => {
		//失败的时候
		if (response.status !== STATUS) {
			console.log('走通接口,但失败的时候', response);
			return Promise.reject('error')
		}
		//成功的时候
		else {
			console.log(
				'走通接口,且成功的时候',
				`${response.config.method.toUpperCase()}`,
				`${response.request.responseURL}\n`,
				response
			);
			return response
		}
	},
	//直接报错的时候
	error => {
		//https://www.jianshu.com/p/349b7ab263f8
		console.log('直接报错的时候', error.response);
		return Promise.reject(error)
	}
)

export default axiosInstance
